﻿/*
 *  Sqlite ORM - PUBLIC DOMAIN LICENSE
 *  Copyright (C)  2010-2012. Ian Quigley
 *  
 *  This source code is provided 'As is'. You bear the risk of using it.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SqliteORM.Tests.Model;
using SubSpec;
using Xunit;

namespace SqliteORM.Tests
{
	public class SelectWhere
	{
		[Specification]
		public void SelectWhereExpressionTree()
		{
			TableIdString testOne = new TableIdString() { Id = 1, Text = "Hello" };
			TableIdString testTwo = new TableIdString() { Id = 2, Text = "Test Two" };
			"Given an table with some known data"
				.Context(() =>
				{
					Context.Initialise();
					DbConnection.Initialise(Context.ConnectionString);
					using (TableAdapter<TableIdString> adapter = TableAdapter<TableIdString>.Open())
					{
						adapter.CreateUpdate(testOne);
						adapter.CreateUpdate(testTwo);
					}
				});

			IEnumerable<TableIdString> results = null;

			"When selecting Where with Expression".Do(
				( ) =>
					{
						using (TableAdapter<TableIdString> adapter = TableAdapter<TableIdString>.Open())
							results = adapter.Select().Where(t => t.Id == 2).ToList();
					} );

			"Then expect correct results".Observation(
				() =>
					{
						Assert.NotNull( results );
						Assert.Equal( 1, results.Count() );
					});

		}

        [Specification]
        public void SelectWhereExpressionTree2z()
        {
            TableIdString testOne = new TableIdString() { Id = 1, Text = "Hello" };
            TableIdString testTwo = new TableIdString() { Id = 2, Text = "Test Two" };
            "Given an table with some known data"
                .Context(() =>
                {
                    Context.Initialise();
                    DbConnection.Initialise(Context.ConnectionString);
                    using (TableAdapter<TableIdString> adapter = TableAdapter<TableIdString>.Open())
                    {
                        adapter.CreateUpdate(testOne);
                        adapter.CreateUpdate(testTwo);
                    }
                });

            IEnumerable<TableIdString> results = null;

            "When selecting Where with Expression".Do(
                () =>
                {
                    using (TableAdapter<TableIdString> adapter = TableAdapter<TableIdString>.Open())
						results = adapter.Select().Where(t => t.Id == 2).ToList();
                });

            "Then expect correct results".Observation(
                () =>
                {
                    Assert.NotNull(results);
                    Assert.Equal(1, results.Count());
                });

        }

    }
}
